package cn.lixin.user.controller;

import cn.lixin.basic.exception.BusinessException;
import cn.lixin.basic.utils.AjaxResult;
import cn.lixin.user.domain.LoginDto;
import cn.lixin.user.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    /**
     * 账号登录
     * @param loginDto
     * @return
     */
    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            Map<String,Object> map =  loginService.accountLogin(loginDto);
            //这里希望service返回token字符串和登录人的信息
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("请勿绕过前端重复点击，稍后重试！！");
        }

    }

    @PostMapping("/wechat")
    public AjaxResult account(@RequestBody Map<String,String> params){
        try {
            return loginService.wechat(params);
            //这里希望service返回token字符串和登录人的信息
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("请勿绕过前端重复点击，稍后重试！！");
        }

    }

    /**
     * 微信登录流程
     * @param params
     * @return
     */
    @PostMapping("/binder/wechat")
    public AjaxResult binder(@RequestBody Map<String,String> params){
        try {
            Map<String,Object> map =  loginService.binder(params);
            //这里希望service返回token字符串和登录人的信息
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！！");
        }

    }

}